<template>
  <div class="tinymce-editor">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick">
    </editor>
  </div>
</template>

<script>
  import tinymce from 'tinymce/tinymce'
  import Editor from '@tinymce/tinymce-vue'
  import 'tinymce/themes/silver/theme'
  import 'tinymce/icons/default'; //引入编辑器图标icon，不引入则不显示对应图标

  import 'tinymce/plugins/image'
  import 'tinymce/plugins/media'
  import 'tinymce/plugins/table'
  import 'tinymce/plugins/lists'
  import 'tinymce/plugins/wordcount'
  import 'tinymce/plugins/colorpicker'
  import 'tinymce/plugins/fullscreen'
  import 'tinymce/plugins/toc' //目录生成器
  import {httpAction} from '../../api/manage'
  export default {
    components: {
      Editor
    },
    props: {
      value: {
        type: String,
        required: false
      },
      triggerChange: {
        type: Boolean,
        default: false,
        required: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      plugins: {
        type: [String, Array],
        default: 'lists image media table  wordcount  fullscreen premium'
      },
      toolbar: {
        type: [String, Array],
        //  media
        default: 'undo redo | premium | formatselect fontselect fontsizeselect | forecolor backcolor| bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image  table | removeformat | fullscreen'
      }
    },
    data() {
      return {
        //初始化配置
        init: {
          language_url: '/tinymce/langs/zh_CN.js',
          language: 'zh_CN',
          skin_url: '/tinymce/skins/lightgray',

          //解决打包后上传图片不带前缀问题
          // convert_urls: false,//自动转换URL 关闭此选项，TinyMCE将不会自动处理URL，也就是说，插入的URL原本是什么就是什么，不会自动转换相对或绝对路径
          // relative_urls : false,//true 转换 false不转换 此设置将把当前域名中的所有URL转换为相对URL。相对位置基于document_base_url的配置。
          // remove_script_host : false,//删除URL的域名部分
          // document_base_url : window._CONFIG['imgDomainURL']+'/',

          height: 500,
          plugins: this.plugins,
          toolbar: this.toolbar,
          branding: false,
          menubar: 'file edit insert view format table tools', //菜单栏配置，设为false则隐藏，不配置则默认显示全部菜单，也可自定义配置
          menu: {
          edit: { title: '编辑', items: 'undo redo | cut copy paste pastetext | selectall' },
          insert: { title: '插入', items: 'link image  |  hr' },
        },
          toc_depth: 3,
          fontsize_formats: '12px 14px 16px 18px 20px 22px 24px 26px 28px 30px 36px 48px 56px 72px',
          font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;',
          // images_upload_handler: (blobInfo, success) => {
          //   const img = 'data:image/jpeg;base64,' + blobInfo.base64()
          //   success(img)
          // }
          // 富文本编辑器里添加图片，这里手动上传并获取图片地址，用图片地址回显图片，不用base64，否则提交的时候数据太长报错。
          images_upload_handler: (blobInfo, success, failure) => {
            if(blobInfo.blob().size/1024/1024>2){
              failure("上传失败，图片大小请控制在 2M 以内")
            }else{
              const formData = new FormData();
              formData.append('file', blobInfo.blob());
              httpAction(window._CONFIG['domianURL'] + '/sys/common/upload',formData,'post').then((res)=>{
                if(res.success){
                  success(window._CONFIG['imgDomainURL'] + '/'+res.message);
                  return
                }else{
                  this.$message.warning(res.message)
                }
              }).catch(()=>{
                this.$message.warning('上传失败')
              })
            }
          }
        },
        myValue: this.value
      }
    },
    mounted() {
      tinymce.init({})
    },
    methods: {

      onClick(e) {
        this.$emit('onClick', e, tinymce)
      },
      //可以添加一些自己的自定义事件，如清空内容
      clear() {
        this.myValue = ''
      }
    },
    watch: {
      value(newValue) {
        this.myValue = newValue
      },
      myValue(newValue) {
        if (this.triggerChange) {
          this.$emit('change', newValue)
        } else {
          this.$emit('input', newValue)
        }
      }
    }
  }

</script>
<style scoped>
</style>